<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="dePForm">
    <el-form-item label="客户名称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="ruleForm.sex">
        <el-radio label="男"></el-radio>
        <el-radio label="女"></el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="ruleForm.phone"></el-input>
    </el-form-item>
    <el-form-item label="微信" prop="weixin">
      <el-input v-model="ruleForm.weixin"></el-input>
    </el-form-item>
    <el-form-item label="QQ" prop="QQ">
      <el-input v-model="ruleForm.QQ"></el-input>
    </el-form-item>
    <el-form-item label="邮编" prop="email">
      <el-input v-model="ruleForm.email"></el-input>
    </el-form-item>
    <el-form-item label="类型" prop="type">
      <el-select v-model="ruleForm.type" clearable placeholder="请选择">
        <el-option label="重点客户" value="重点客户"></el-option>
        <el-option label="一般客户" value="一般客户"></el-option>
        <el-option label="放弃客户" value="放弃客户"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input type="textarea" v-model="ruleForm.address" :rows="4"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建/更新</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
      <el-button @click="back">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {addCustom, getCustomInfo,updataCustom} from "../../../api/api";

export default {
  data() {
    return {
      name: "addCustom",
      ruleForm: {
        name: '',
        sex: '',
        phone: '',
        weixin: '',
        email: '',
        QQ: '',
        type: '',
        typeArr: [],
        address: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入客户电话', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ],
        weixin: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        QQ: [
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ],
        email: [
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择客户类型', trigger: 'change' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请填写客户地址', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.id = this.$route.query.id
    this.id && this.getCustomInfo()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let sex = this.ruleForm.sex === '男'? 0 : 1

          if (this.id) {
            updataCustom({
              customerId: this.id,
              name: this.ruleForm.name,
              sex: sex,
              email: this.ruleForm.email,
              phone: this.ruleForm.phone,
              QQ: this.ruleForm.QQ,
              weixin: this.ruleForm.weixin,
              type: this.ruleForm.type,
              address: this.ruleForm.address
            }).then(data => {
              if (data.code ===0) {
                // 修改成功
                this.$alert('修改成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    // 点击确定我们跳转到列表页
                    this.$router.push('/crm/custom')
                  }
                })
              } else {
                this.$alert('修改失败', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {}
                })
              }
            })
          } else {
            addCustom({
              name: this.ruleForm.name,
              sex: sex,
              email: this.ruleForm.email,
              phone: this.ruleForm.phone,
              QQ: this.ruleForm.QQ,
              weixin: this.ruleForm.weixin,
              type: this.ruleForm.type,
              address: this.ruleForm.address
            }).then(data => {
              if (data.code ===0) {
                // 新增成功
                this.$alert('新增成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    // 点击确定我们跳转到列表页
                    this.$router.push('/crm/custom')
                  }
                })
              } else {
                this.$alert('新增失败', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {}
                })
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    // 获取用户详细信息
    getCustomInfo() {
      let id = this.$route.query.id
      getCustomInfo(id).then(data => {
        let sex = data.data.sex === 0? '男' : '女'
        this.ruleForm.name = data.data.name
        this.ruleForm.sex = sex
        this.ruleForm.email = data.data.email
        this.ruleForm.phone = data.data.phone
        this.ruleForm.QQ = data.data.QQ
        this.ruleForm.weixin = data.data.weixin
        this.ruleForm.type = data.data.type
        this.ruleForm.address = data.data.address
      })
    },

    back() {
      this.$router.back()
    }
  }
}
</script>

<style scoped>
  .dePForm {
    text-align: left;
    width: 650px;
  }
</style>
